package com.kidback.bbloc259.util;

import android.app.Activity;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.kidback.bbloc259.app.KidApplication;
import com.kidback.bbloc259.base.lang.Delay;
import com.kidback.bbloc259.base.lang.Runnable0;
import com.mylhyl.circledialog.BaseCircleDialog;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.callback.ConfigText;
import com.mylhyl.circledialog.callback.ConfigTitle;
import com.mylhyl.circledialog.params.ProgressParams;
import com.mylhyl.circledialog.params.TextParams;
import com.mylhyl.circledialog.params.TitleParams;
import com.mylhyl.circledialog.view.listener.OnButtonClickListener;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by meikai on 2020/06/12.
 */
public class TipUtils {

    public static Disposable showMsg(Activity activity, String msg) {
        return showLoadingShotDialog(activity, msg);
    }

    public static Disposable showLoading(Activity activity, String msg) {
        return showLoadingShotDialog(activity, msg);
    }

    public static Disposable showSuccess(String msg) {
        return showSuccess(KidApplication.getCurrentActivity(), msg);
    }

    public static Disposable showSuccess(Activity activity, String msg) {

        if (!(activity instanceof AppCompatActivity)) {
            ToastUtils.show(msg);
            return null;
        }

        BaseCircleDialog dialog = new CircleDialog.Builder()
                .setText(msg)
                .setWidth(0.7f)
                .show(((AppCompatActivity) activity).getSupportFragmentManager());

        return Observable.timer(1500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (dialog.getContext() instanceof Activity && ((Activity) dialog.getContext()).isDestroyed()) {
                            return;
                        }
                        if (dialog.isAdded()) {
                            dialog.dismiss();
                        }
                    }
                });
    }

    public static void showMsgFixed(Activity activity, String msg) {
        showMsgFixed(activity, "系统提示", msg);
    }

    public static void showMsgFixedCenterText(Activity activity, String msg) {
        showMsgFixed(activity, msg, Gravity.CENTER);
    }

    public static void showMsgFixed(Activity activity, String title, String msg) {
        if (!(activity instanceof AppCompatActivity)) {
            ToastUtils.show(msg);
            return;
        }

        BaseCircleDialog dialog = new CircleDialog.Builder()
                .configTitle(new ConfigTitle() {
                    @Override
                    public void onConfig(TitleParams params) {
                        params.text = title;
                        params.textSize = 16;
                        params.styleText = Typeface.BOLD;
                    }
                })
                .configText(new ConfigText() {
                    @Override
                    public void onConfig(TextParams params) {
                        params.text = msg;
                        params.textColor = 0xff333333;
                        params.gravity = Gravity.LEFT;
                        params.textSize = 13;

                    }
                })
                .setWidth(0.6f)
                .show(((AppCompatActivity) activity).getSupportFragmentManager());
    }

    public static void showMsgFixed(Activity activity, String msg, String btnText, OnButtonClickListener onButtonClickListener) {
        showMsgFixed(activity, msg, btnText, true, onButtonClickListener);
    }

    public static void showMsgFixed(Activity activity, String msg, String btnText, boolean cancelable, OnButtonClickListener onButtonClickListener) {
        showMsgFixed(activity, "系统提示", msg, btnText, cancelable, onButtonClickListener);
    }

    public static void showMsgFixed(Activity activity, String title, String msg, String btnText, boolean cancelable,
                                    OnButtonClickListener onButtonClickListener) {
        if (!(activity instanceof AppCompatActivity)) {
            ToastUtils.show(msg);
            return;
        }

        BaseCircleDialog dialog = new CircleDialog.Builder()
                .configTitle(new ConfigTitle() {
                    @Override
                    public void onConfig(TitleParams params) {
                        params.text = title;
                        params.textSize = 16;
                        params.styleText = Typeface.BOLD;
                    }
                })
                .setCancelable(cancelable)
                .setCanceledOnTouchOutside(cancelable)
                .configText(new ConfigText() {
                    @Override
                    public void onConfig(TextParams params) {
                        params.text = msg;
                        params.textColor = 0xff333333;
                        params.gravity = Gravity.CENTER;
                        params.textSize = 13;

                    }
                })
                .setWidth(0.7f)
                .setPositive(btnText, new OnButtonClickListener() {
                    @Override
                    public boolean onClick(View v) {
                        if (onButtonClickListener != null) {
                            return onButtonClickListener.onClick(v);
                        }
                        return true;
                    }
                })
                .show(((AppCompatActivity) activity).getSupportFragmentManager());
    }

    public static void showMsgFixed(Activity activity, String msg, int gravity) {
        if (!(activity instanceof AppCompatActivity)) {
            ToastUtils.show(msg);
            return;
        }

        BaseCircleDialog dialog = new CircleDialog.Builder()
                .configTitle(new ConfigTitle() {
                    @Override
                    public void onConfig(TitleParams params) {
                        params.text = "系统提示";
                        params.textSize = 16;
                        params.styleText = Typeface.BOLD;
                    }
                })
                .configText(new ConfigText() {
                    @Override
                    public void onConfig(TextParams params) {
                        params.text = msg;
                        params.textColor = 0xff333333;
                        params.gravity = gravity;
                        params.textSize = 13;

                    }
                })
                .setWidth(0.7f)
                .show(((AppCompatActivity) activity).getSupportFragmentManager());
    }

    public static void showMsgFixed(Activity activity, int gravity, String title, String msg) {
        if (!(activity instanceof AppCompatActivity)) {
            ToastUtils.show(msg);
            return;
        }

        BaseCircleDialog dialog = new CircleDialog.Builder()
                .configTitle(new ConfigTitle() {
                    @Override
                    public void onConfig(TitleParams params) {
                        params.text = title;
                        params.textSize = 16;
                        params.styleText = Typeface.BOLD;
                    }
                })
                .configText(new ConfigText() {
                    @Override
                    public void onConfig(TextParams params) {
                        params.text = msg;
                        params.textColor = 0xff333333;
                        params.gravity = gravity;
                        params.textSize = 13;

                    }
                })
                .setWidth(0.7f)
                .show(((AppCompatActivity) activity).getSupportFragmentManager());
    }

    public static void showMsgDuration(Activity activity, String msg, int gravity, long duration) {
        if (!(activity instanceof AppCompatActivity)) {
            ToastUtils.show(msg);
            return;
        }

        BaseCircleDialog dialog = new CircleDialog.Builder()
                .configTitle(new ConfigTitle() {
                    @Override
                    public void onConfig(TitleParams params) {
                        params.text = "系统提示";
                        params.textSize = 16;
                        params.styleText = Typeface.BOLD;
                    }
                })
                .configText(new ConfigText() {
                    @Override
                    public void onConfig(TextParams params) {
                        params.text = msg;
                        params.textColor = 0xff333333;
                        params.gravity = gravity;
                        params.textSize = 13;

                    }
                })
                .setWidth(0.7f)
                .show(((AppCompatActivity) activity).getSupportFragmentManager());

        Delay.delay(duration, new Runnable0() {
            @Override
            public void run() {
                if (dialog.getContext() instanceof Activity && ((Activity) dialog.getContext()).isDestroyed()) {
                    return;
                }

                if (dialog.isAdded()) {
                    dialog.dismiss();
                }
            }
        });
    }

    public static Disposable showFail(Activity activity, String msg) {
        return showLoadingShotDialog(activity, msg);
    }

    public static Disposable showInfo(Activity activity, String msg) {
        return showLoadingShotDialog(activity, msg);
    }

    private static Disposable showLoadingShotDialog(Activity activity, String msg) {
        if (!(activity instanceof AppCompatActivity)) {
            ToastUtils.show(msg);
            return null;
        }

        BaseCircleDialog dialog = new CircleDialog.Builder()
                //添加标题，参考普通对话框
                .setProgressStyle(ProgressParams.STYLE_SPINNER)//STYLE_HORIZONTAL 或 STYLE_SPINNER
                .setProgressText(msg)// style = 水平样式时，text 支持 String.format() 例如：已经下载%s
                //添加按钮，参考普通对话框
                .show(((AppCompatActivity) activity).getSupportFragmentManager());


        return Observable.timer(1500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (dialog.getContext() instanceof Activity && ((Activity) dialog.getContext()).isDestroyed()) {
                            return;
                        }

                        if (dialog.isAdded()) {
                            dialog.dismiss();
                        }
                    }
                });
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////
    private static BaseCircleDialog loadingTipDialog;

    public static void showLoadingFixDialog(Activity activity, String msg) {
        if (!(activity instanceof AppCompatActivity)) {
            ToastUtils.show(msg);
            return;
        }

        loadingTipDialog = new CircleDialog.Builder()
                //添加标题，参考普通对话框
                .setWidth(0.7f)
                .setProgressStyle(ProgressParams.STYLE_SPINNER)//STYLE_HORIZONTAL 或 STYLE_SPINNER
                .setProgressText(msg)// style = 水平样式时，text 支持 String.format() 例如：已经下载%s
                //添加按钮，参考普通对话框
                .show(((AppCompatActivity) activity).getSupportFragmentManager());
    }

    public static void hideLoadingFixDialog() {
        if (loadingTipDialog == null) {
            return;
        }

        if (loadingTipDialog.getContext() instanceof Activity && ((Activity) loadingTipDialog.getContext()).isDestroyed()) {
            return;
        }

        if (loadingTipDialog.isAdded()) {
            loadingTipDialog.dismiss();
        }
    }
    //////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}
